package com.nyw.wanandroid.module.knowledge.mvp;

import com.bakerj.rxretrohttp.RxRetroHttp;
import com.nyw.domain.common.api.WanApiResult;
import com.nyw.domain.common.loadmore.PageLoadMoreResponse;
import com.nyw.domain.domain.bean.request.know.KnowReq;
import com.nyw.domain.domain.bean.response.home.ArticleBean;
import com.nyw.domain.domain.event.home.CollectionEvent;
import com.nyw.libproject.common.api.CBApiObserver;
import com.nyw.wanandroid.module.home.data.repository.IhomeRepository;
import com.nyw.wanandroid.module.home.data.repository.homeRepositoryImpl;
import com.nyw.wanandroid.module.home.mvp.HomeContract;
import com.nyw.wanandroid.module.knowledge.data.repository.IknowledgeRepository;
import com.nyw.wanandroid.module.knowledge.data.repository.knowledgeRepositoryImpl;

import java.util.List;

import io.reactivex.Observable;

/**
 * @author nyw
 * @date 2019/07/10
 *
 * Generated by MVPGenerator
 */
public class KnowDetailPresenter extends KnowDetailContract.Presenter{
    private IknowledgeRepository mRepository = new knowledgeRepositoryImpl();
    private IhomeRepository Repository = new homeRepositoryImpl();

    private int cid;
    public KnowDetailPresenter(KnowDetailContract.View view,int cid) {
        super(view);
        this.cid=cid;
    }

    @Override
    protected KnowReq getQuestBody() {
        KnowReq req = new KnowReq();
        req.setCid(cid);
        return req;
    }

    @Override
    protected void setUpRefreshBody(KnowReq body) {
        super.setUpRefreshBody(body);
        body.setCid(cid);
    }

    @Override
    protected ArticleBean castDataToDest(ArticleBean articleBean) {
        return articleBean;
    }

    @Override
    protected Observable<List<ArticleBean>> getRefreshLoadObservable(KnowReq body) {
        return mRepository.getArticleBean(body).map(PageLoadMoreResponse::getDatas);
    }

    @Override
    public void Collect(int id) {
        RxRetroHttp.composeRequest(Repository.Collect(id), mView)
                .subscribe(new CBApiObserver<WanApiResult>() {
                    @Override
                    protected void success(WanApiResult data) {
                        ((KnowDetailContract.View) mView).CollectSuccess();
                        CollectionEvent.postCollectWithArticleId(id);
                    }
                });
    }

    @Override
    public void UnCollect(int id) {
        RxRetroHttp.composeRequest(Repository.Uncollect(id), mView)
                .subscribe(new CBApiObserver<WanApiResult>() {
                    @Override
                    protected void success(WanApiResult data) {
                        ((KnowDetailContract.View) mView).UnCollectSuccess();
                        CollectionEvent.postUnCollectWithArticleId(id);
                    }
                });
    }
}

